home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2987 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.1 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP!!! - loop control problems
  5. Date: 21 Jan 1996 20:31:54 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4dt16a$7md@werple.net.au>
  8. References: <4ds174$6t0@bolivia.it.earthlink.net>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. macc@earthlink.net (OB1) writes:
  12.  
  13. >I have the following code in a program I'm writing:
  14.  
  15. >const long SCREEN_SIZE = 320 * 200;
  16. >long count = 0;
  17. >while (count <= SCREEN_SIZE)
  18. >{
  19. >    ... code (count gets incremented)
  20. >}
  21.  
  22. >When I trace through my program, I find that the loop end condition is
  23. >always true and the code inside the loop is never run. I looking for
  24. >any ideas on why this doesn't work.
  25.  
  26. Because you are multiplying two 'int' types, and the result has 
  27. overflowed the capacity of an 'int' to give a negative number. The 
  28. negative number is then converted to 'long'. For a compiler using 32-bit 
  29. 'int's this would have worked; your compiler is probably using 16 bits.
  30. (The highest 16-bit integer is 32767; 320*200 is 64000).
  31.  
  32. Try this instead:
  33.  
  34. const long SCREEN_SIZE = 320L * 200;
  35.  
  36. David White
  37. davidw@werple.mira.net.au
  38.